﻿package plugins.mstandio.UniversalMap.cameras {
	 	
	/**
	 * ...
	 * @author mstandio
	 */		
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;	
	
	import org.papervision3d.objects.primitives.Cone;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Cylinder;
	
	import org.papervision3d.render.BasicRenderEngine;	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.scenes.Scene3D;	
	import org.papervision3d.objects.DisplayObject3D;
	
	import org.papervision3d.materials.ColorMaterial;	
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.lights.PointLight3D;	
		
	
	public class Cam_camera extends Cam {					
		
		private var tape1:Cylinder;
		private var tape2:Cylinder;
		private var bulk:Cube;		
		private var cone:Cone;
		private var lense:Cylinder;		
		private var tripod:DisplayObject3D;
		private var tripodCone:DisplayObject3D;
		
		private var color:ColorMaterial;		
		private var flat_bulk:FlatShadeMaterial;
		private var flat_lense:FlatShadeMaterial;
		private var flat_tape:FlatShadeMaterial;
		private var light:PointLight3D;
				
		private var camera:Camera3D;
		private var viewport:Viewport3D; 
		private var renderer:BasicRenderEngine;
		private var scene:Scene3D;
		
				
		public function Cam_camera(initPan:Number, initTilt:Number, initZoom:Number, camSize:Number, panShift:Number, camColor:String) :void {
			super(initPan, initTilt, initZoom, camSize, panShift, camColor);			
			if (stage) stageReady();
			else addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);      
		}
		
		private function stageReady(e:Event = null):void {            
			removeEventListener(Event.ADDED_TO_STAGE, stageReady);							
			this.viewport = new Viewport3D(350, 350, false, false, false, true); 
			
			this.viewport.x = this.viewport.y = -175;
			this.addChild(viewport);
			
			this.renderer = new BasicRenderEngine();									
			this.scene = new Scene3D();
			
			this.camera = new Camera3D();
			this.camera.zoom = 13*this.camSize; 
			this.camera.focus = 100; 						                         
			
			this.color = new ColorMaterial(Number(this.camColor), 0.7);				
			light = new PointLight3D(true); light.z = -300; 
			//flat_bulk   = new FlatShadeMaterial(light, 0x666666, 0x222222);
			//flat_lense  = new FlatShadeMaterial(light, 0x444444, 0x222222);
			//flat_lense.doubleSided = true;
			//flat_tape   = new FlatShadeMaterial(light, 0x999999, 0x111111);			
			flat_bulk   = new FlatShadeMaterial(light, 0x444444, 0x222222);
			flat_lense  = new FlatShadeMaterial(light, 0x444444, 0x222222);
			flat_lense.doubleSided = true;
			flat_tape   = new FlatShadeMaterial(light, 0x444444, 0x222222);
			var materials :MaterialsList = new MaterialsList( { all : flat_bulk } );			
			
			bulk = new Cube(materials, 20, 15, 8, 1, 1, 1, 0, 0);						
			
			
			lense = new Cylinder(flat_lense, 4, 8, 4, 1, 8, false,true);									
			lense.rotationZ += 90;
			lense.rotationY += 45;
			lense.x -= 14.25;
			lense.z -= 3;			
			
			bulk.addChild(lense);
			
			tape1 = new Cylinder(flat_tape, 8, 5, 16, 1, -1, true, true);
			tape1.rotationY += 45;
			tape1.x -= 7;
			tape1.z -= 15;						
			bulk.addChild(tape1);
			
			tape2 = new Cylinder(flat_tape , 8, 5, 16, 1, -1, true, true);
			tape2.rotationY += 45;
			tape2.x += 8;
			tape2.z -= 15
			bulk.addChild(tape2);
			
			tripod = new DisplayObject3D();
			tripod.addChild(bulk);
			scene.addChild(tripod);			
			tripod.rotationX += 20;				
			
			tripodCone = new DisplayObject3D();
			bulk.addChild(tripodCone);				
			tripodCone.scaleZ = stage.stageHeight/stage.stageWidth +0.2;						
						
			this.rotate(this.initPan, this.initTilt);						
			this.drawCone(this.initZoom);				
			
		}		
		
		
		
		private function drawCone(zoom:Number):void {			
			if(this.cone != null){				
				this.tripodCone.removeChild(cone);
				this.cone = null;
			}			
			this.cone = new Cone(color, 3.5 * (20-zoom), 35, 4, 1);						
			this.cone.x -= 37;			
			this.cone.z -=3;
			this.cone.rotationZ -= 90;
			this.cone.rotationY -= 45;
			this.tripodCone.addChild(this.cone);				
			
			this.renderer.renderScene(scene, camera, viewport);				
		}		
		
		
		public override function rotate(pan:Number, tilt:Number):void {									
			this.bulk.rotationZ = - pan - this.panShift +180;
			this.bulk.rotationY = tilt;
			this.renderer.renderScene(scene, camera, viewport);				
			this.initPan = pan;
			this.initTilt = tilt;
		}
		
		public override function zoom(zoom:Number):void {						
			this.initZoom = this.validateZoom(zoom);
			this.drawCone(this.initZoom);			
		}				
	}	
}